package com.crm.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.sys.dao.ApproveConfigureDao;
import com.crm.sys.model.ApproveConfigure;
import com.crm.sys.model.ApproveConfigureItem;
import com.crm.sys.service.ApproveConfigureService;

@Service("approveConfigureService")
public class ApproveConfigureServiceImpl implements ApproveConfigureService {
	
	@Autowired
	private ApproveConfigureDao approveConfigureDao;

	@Override
	public List<ApproveConfigure> getAllApproveConfigureList(ApproveConfigure ac,int pageNo,int pageSize) {
		ac.setStartsize((pageNo-1)*pageSize);
		ac.setEndsize(pageSize);
		return approveConfigureDao.getAllApproveConfigureList(ac);
	}

	@Override
	public List<ApproveConfigureItem> getApproveUserListByAcid(ApproveConfigureItem aci,int pageNo,int pageSize) {
		
		aci.setStartsize((pageNo-1)*pageSize);
		aci.setEndsize(pageSize);
		return approveConfigureDao.getApproveUserListByAcid(aci);
	}

	@Override
	public int getApproveUserCount(Integer acid) {
		ApproveConfigureItem aci = new ApproveConfigureItem();
		aci.setAcid(acid);
		return approveConfigureDao.getApproveUserCount(aci);
	}

	@Override
	public int updateApproveUser(ApproveConfigureItem aci, Integer type) {
		aci.setParentid(0);
		aci.setState(0);
		//下面开始判断情况：
		if (type == 0) {
			//新增的情况：
			if (aci.getLevel() == aci.getOldlevel()) {
				//位置不变，只需新增自己
				approveConfigureDao.insertApproveUser(aci);
			} else {
				//位置改变，修改位置改变后的所有记录后新增
				//changetype = 1
				aci.setChangetype(1);
				List<ApproveConfigureItem> changeList = approveConfigureDao.getApproveUserChanegList(aci);
				for (ApproveConfigureItem acit : changeList) {
					//level+1
					acit.setLevel(acit.getLevel()+1);
					approveConfigureDao.updateApproveUser(acit);
				}
				//最后把新增的加入
				approveConfigureDao.insertApproveUser(aci);
			} 
		} else {
			//修改的情况：
			if (aci.getLevel() == aci.getOldlevel()) {
				//位置不变，只需修改自己
				approveConfigureDao.updateApproveUser(aci);
			} else if (aci.getLevel() > aci.getOldlevel()) {
				//位置向下移动，修改区间内容
				//changetype = 2
				aci.setChangetype(2);
				List<ApproveConfigureItem> changeList = approveConfigureDao.getApproveUserChanegList(aci);
				for (ApproveConfigureItem acit : changeList) {
					//level-1
					acit.setLevel(acit.getLevel()-1);
					approveConfigureDao.updateApproveUser(acit);
				}
				//最后修改
				approveConfigureDao.updateApproveUser(aci);
			} else {
				//位置向上移动，修改区间内容
				//changetype = 3
				aci.setChangetype(3);
				List<ApproveConfigureItem> changeList = approveConfigureDao.getApproveUserChanegList(aci);
				for (ApproveConfigureItem acit : changeList) {
					//level +1
					acit.setLevel(acit.getLevel()+1);
					approveConfigureDao.updateApproveUser(acit);
				}
				//最后修改
				approveConfigureDao.updateApproveUser(aci);
			}
		}
		
		return 1;
	}

	@Override
	public int deleteApproveUser(ApproveConfigureItem aci) {
		
		//删除后，还涉及到等级的变化，因为是删除，所以从这个level以下，每个level-1
		//changetype = 1
		aci.setChangetype(1);
		List<ApproveConfigureItem> changeList = approveConfigureDao.getApproveUserChanegList(aci);
		for (ApproveConfigureItem acit : changeList) {
			//level-1
			acit.setLevel(acit.getLevel()-1);
			approveConfigureDao.updateApproveUser(acit);
		}
		//删除该记录
		approveConfigureDao.deleteApproveUser(aci);
		return 1;
	}

}
